﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;
public class PlayerInfo : MonoBehaviour {

    public GameObject playerPanel;
    public GameObject closeBtn;
    public GameObject playerItemPre;
    public GameObject grid;

    public UILabel numLabel;
    public List<GameUserInfo> userList;
	void Start () {
        UIEventListener.Get(closeBtn).onClick = ButtonClick;

        Debug.Log("站立玩家的数量 = " + userList.Count);
        LogMsg();
        //无座玩家个数        
        numLabel.text = (userList.Count).ToString();
	}

    void ButtonClick(GameObject button)
    {
        if(button.name == "CloseBtn"){
            Destroy(playerPanel);
        }
    }

    void LogMsg()
    {
        for (int i = 0; i < userList.Count;i++ )
        {
            GameObject playerItem = Instantiate(playerItemPre);
            playerItem.transform.parent = grid.transform;
            playerItem.transform.localScale = Vector3.one;
            PlayerItem player = playerItem.GetComponent<PlayerItem>();

            if(userList[i].AvatarUrl != ""){
                WXAvatarManager._instance.GetWxAvatar(userList[i].Uid,player.wxAvatar,userList[i].AvatarUrl);
                player.wxAvatar.gameObject.SetActive(true);
            }
            else
            {
                player.avatar.spriteName = userList[i].AvatarId.ToString();               
            }
            player.nameLabel.text = userList[i].Nickname;
            player.coinLabel.text = EginUser.Instance.ReturnMoney(userList[i].BagCoinCount);
            player.uid = userList[i].Uid;
            //StartCoroutine(GetWWWFace(player.wxAvatar,userList[i].AvatarUrl));
        }
    }

    /// <summary>
    /// 加载微信头像
    /// </summary>
    /// <param name="texture"></param>
    /// <param name="url"></param>
    /// <returns></returns>
    IEnumerator GetWWWFace(UITexture texture, string url)
    {
        if (url != "")
        {
            WWW www = new WWW(url);
            yield return www;
            if (www.isDone)
            {
                if (www.error == null)
                {

                }
                texture.mainTexture = www.texture;
            }
            else
            {

            }
            www.Dispose();
        }
    }
}
